namespace DotNetNuke.Entities.Portals
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;

    public class PortalAliasController
    {
        public int AddPortalAlias(PortalAliasInfo objPortalAliasInfo)
        {
            DataCache.ClearHostCache(false);
            return DataProvider.Instance().AddPortalAlias(objPortalAliasInfo.PortalID, objPortalAliasInfo.HTTPAlias);
        }

        public void DeletePortalAlias(int PortalAliasID)
        {
            DataCache.ClearHostCache(false);
            DataProvider.Instance().DeletePortalAlias(PortalAliasID);
        }

        public PortalAliasInfo GetPortalAlias(string PortalAlias, int PortalID)
        {
            return (PortalAliasInfo) CBO.FillObject(DataProvider.Instance().GetPortalAlias(PortalAlias, PortalID), typeof(PortalAliasInfo));
        }

        public ArrayList GetPortalAliasArrayByPortalID(int PortalID)
        {
            ArrayList GetPortalAliasArrayByPortalID;
            IDataReader dr = DataProvider.Instance().GetPortalAliasByPortalID(PortalID);
            try
            {
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    PortalAliasInfo objPortalAliasInfo = new PortalAliasInfo();
                    objPortalAliasInfo.PortalAliasID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["PortalAliasID"]));
                    objPortalAliasInfo.PortalID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["PortalID"]));
                    objPortalAliasInfo.HTTPAlias = Convert.ToString(RuntimeHelpers.GetObjectValue(dr["HTTPAlias"])).ToLower();
                    arr.Add(objPortalAliasInfo);
                }
                GetPortalAliasArrayByPortalID = arr;
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return GetPortalAliasArrayByPortalID;
        }

        public PortalAliasInfo GetPortalAliasByPortalAliasID(int PortalAliasID)
        {
            return (PortalAliasInfo) CBO.FillObject(DataProvider.Instance().GetPortalAliasByPortalAliasID(PortalAliasID), typeof(PortalAliasInfo));
        }

        public PortalAliasCollection GetPortalAliasByPortalID(int PortalID)
        {
            PortalAliasCollection GetPortalAliasByPortalID;
            IDataReader dr = DataProvider.Instance().GetPortalAliasByPortalID(PortalID);
            try
            {
                PortalAliasCollection objPortalAliasCollection = new PortalAliasCollection();
                while (dr.Read())
                {
                    PortalAliasInfo objPortalAliasInfo = new PortalAliasInfo();
                    objPortalAliasInfo.PortalAliasID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["PortalAliasID"]));
                    objPortalAliasInfo.PortalID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["PortalID"]));
                    objPortalAliasInfo.HTTPAlias = Convert.ToString(RuntimeHelpers.GetObjectValue(dr["HTTPAlias"]));
                    objPortalAliasCollection.Add(Convert.ToString(RuntimeHelpers.GetObjectValue(dr["HTTPAlias"])).ToLower(), objPortalAliasInfo);
                }
                GetPortalAliasByPortalID = objPortalAliasCollection;
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return GetPortalAliasByPortalID;
        }

        public PortalAliasCollection GetPortalAliases()
        {
            return this.GetPortalAliasByPortalID(-1);
        }

        public PortalInfo GetPortalByPortalAliasID(int PortalAliasId)
        {
            return (PortalInfo) CBO.FillObject(DataProvider.Instance().GetPortalByPortalAliasID(PortalAliasId), typeof(PortalInfo));
        }

        public void UpdatePortalAliasInfo(PortalAliasInfo objPortalAliasInfo)
        {
            DataCache.ClearHostCache(false);
            DataProvider.Instance().UpdatePortalAliasInfo(objPortalAliasInfo.PortalAliasID, objPortalAliasInfo.PortalID, objPortalAliasInfo.HTTPAlias);
        }
    }
}

